/**
 *  Copyright 2009 Henning Teek, Ingo Renner
 *  
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *  
 *  http://www.apache.org/licenses/LICENSE-2.0
 *  
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License. 
 */
package com.googlecode.chigs.domain;

import java.util.List;

import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;

import org.synyx.hades.domain.AbstractPersistable;

/**
 * @author Ingo Renner
 * @author Henning Teek
 */
@Entity
@NamedQueries({
	@NamedQuery(name = "ConfigPair.findByAll", query = "from ConfigPair o"),
	@NamedQuery(name = "ConfigPair.findByRevision", query = "select o from ConfigPair o, in (o.revisions) r where r = ?")})
public class ConfigPair extends AbstractPersistable<Long> {
	public enum ConfigPairType {
		STRING, NUMBER, BOOLEAN
	};
	private String name;
	private String defaultData;
	private boolean mandatory;

	@Enumerated(EnumType.STRING)
	private ConfigPairType type;

	@ManyToOne
	private ConfigLocation configLocation;
	
	@ManyToMany(mappedBy="configPairs")
	private List<Revision> revisions;

	@ManyToMany
    @JoinTable(name="ConfigPair_Dependency",
            joinColumns=@JoinColumn(name="configPair_id", referencedColumnName="id"),
            inverseJoinColumns=@JoinColumn(name="dependency_id", referencedColumnName="id"))
	private List<ConfigPair> dependencies;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public void setDefaultData(String defaultData) {
		this.defaultData = defaultData;
	}

	public String getDefaultData() {
		return defaultData;
	}

	public boolean isMandatory() {
		return mandatory;
	}

	public void setMandatory(boolean mandatory) {
		this.mandatory = mandatory;
	}

	public ConfigPairType getType() {
		return type;
	}

	public void setType(ConfigPairType type) {
		this.type = type;
	}

	public ConfigLocation getConfigLocation() {
		return configLocation;
	}

	public void setConfigLocation(ConfigLocation configLocation) {
		this.configLocation = configLocation;
	}

	public void setRevisions(List<Revision> revisions) {
		this.revisions = revisions;
	}

	public List<Revision> getRevisions() {
		return revisions;
	}

	public List<ConfigPair> getDependencies() {
		return dependencies;
	}

	public void setDependencies(List<ConfigPair> dependencies) {
		this.dependencies = dependencies;
	}

	@Override
	public String toString() {
		return String
				.format(
						"ConfigPair [configLocation=%s, defaultData=%s, mandatory=%s, name=%s, type=%s, getId()=%s]",
						configLocation, defaultData, mandatory, name, type, getId());
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = super.hashCode();
		result = prime * result
				+ ((configLocation == null) ? 0 : configLocation.hashCode());
		result = prime * result
				+ ((defaultData == null) ? 0 : defaultData.hashCode());
		result = prime * result
				+ ((dependencies == null) ? 0 : dependencies.hashCode());
		result = prime * result + (mandatory ? 1231 : 1237);
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		result = prime * result
				+ ((revisions == null) ? 0 : revisions.hashCode());
		result = prime * result + ((type == null) ? 0 : type.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj) {
			return true;
		}
		if (!super.equals(obj)) {
			return false;
		}
		if (!(obj instanceof ConfigPair)) {
			return false;
		}
		ConfigPair other = (ConfigPair) obj;
		if (configLocation == null) {
			if (other.configLocation != null) {
				return false;
			}
		} else if (!configLocation.equals(other.configLocation)) {
			return false;
		}
		if (defaultData == null) {
			if (other.defaultData != null) {
				return false;
			}
		} else if (!defaultData.equals(other.defaultData)) {
			return false;
		}
		if (dependencies == null) {
			if (other.dependencies != null) {
				return false;
			}
		} else if (!dependencies.equals(other.dependencies)) {
			return false;
		}
		if (mandatory != other.mandatory) {
			return false;
		}
		if (name == null) {
			if (other.name != null) {
				return false;
			}
		} else if (!name.equals(other.name)) {
			return false;
		}
		if (revisions == null) {
			if (other.revisions != null) {
				return false;
			}
		} else if (!revisions.equals(other.revisions)) {
			return false;
		}
		if (type == null) {
			if (other.type != null) {
				return false;
			}
		} else if (!type.equals(other.type)) {
			return false;
		}
		return true;
	}
}
